


#
import requests as req
from bs4 import BeautifulSoup
import json


def get_request_text(url , headers):
    response = req.get(url = url , headers = headers)
    #response = req.get(url = url)
    response.encoding = response.apparent_encoding
    status_code = response.status_code
    print(f"status_code={status_code}")
    if status_code == 200:
        text = response.text
        #print(f"text={text}")
        text = text[text.index('{"result":{"'):text.index(');')]
        text = text.replace('"[\\"' , '[\\"')
        text = text.replace('"]"' , '"]')
        text = text.replace('"{\\"' , '{\\"')
        text = text.replace('"}"' , '"}')
        print(f"text.json={text}")
        return response.text
    else:
        print(f"下载失败")
        return None


def get_data(text):
        result = json.loads(text)
        for data in result['result']['data']:
            print(f"data:{data.text},{data['url']} ,{data['pic']}")




def json_test():
    # 假设你有一个JSON格式的字符串
    #json_str = '{"name": "John", "age": 30, "city": "New York"}'
    json_str = '{"name": "John", "age": 30, "city": "New York" , "wapurls":[\"https:/finance.sina.cnhy/2024-08-17/detail-incixywh3373290.d.html\"]}'

    # 使用json.loads()函数将字符串转换为Python对象（这里是一个字典）
    data = json.loads(json_str)
    print(data)




def writer_file(path , data):
    with open(file=path , mode='wt' , encoding='utf8') as f:
        f.write(data)



#2024年8月17日 全部滚动新闻
#url = 'https://news.sina.com.cn/roll/#pageid=153&lid=2509&k=&num=50&page=1'
# url = 'https://news.sina.com.cn/roll'
url = "https://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid=2509&k=&num=50&page=2&r=0.20335103619894013&callback=jQuery111208297040018330051_1723898061921&_=1723898061923"
# 设置请求头，使其看起来像是从浏览器发出的
# headers = {
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
#     # 可以根据需要添加更多的请求头
# }
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
}


json_test()
reponse_text =  get_request_text(url , headers)
get_data(reponse_text)
#standings(reponse_text)
# result = get_data_str(reponse_text)
# writer_file('data_news.txt' , result)
# result = get_standings_str(reponse_text)
# writer_file('data.txt' , result)

